package logic

import (
	"context"
	"xialou_platform/app/errorx"
	"xialou_platform/services/order/model"

	"xialou_platform/services/order/rpc/internal/svc"
	"xialou_platform/services/order/rpc/orderClient"

	"github.com/zeromicro/go-zero/core/logx"
)

type OrderByOrderNumLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewOrderByOrderNumLogic(ctx context.Context, svcCtx *svc.ServiceContext) *OrderByOrderNumLogic {
	return &OrderByOrderNumLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 根据微信订单号查询订单
func (l *OrderByOrderNumLogic) OrderByOrderNum(in *orderClient.OrderByOrderNumReq) (*orderClient.OrderParam, error) {
	order, err := l.svcCtx.OrderModel.FindOneByOrderNum(l.ctx, in.OrderNum)
	if err != nil {
		if err == model.ErrNotFound {
			return nil, errorx.NewDefaultError("订单不存在。")
		}
		return nil, errorx.NewReasonError("网络出小差了，请重试刷新再试。", err.Error())
	}

	return &orderClient.OrderParam{
		Id:            order.Id,
		Type:          order.Type,
		SourceId:      order.SourceId,
		Money:         order.Money,
		Uid:           order.Uid,
		Status:        order.Status,
		OrderNum:      order.OrderNum,
		TradeType:     order.TradeType,
		PayType:       order.PayType,
		TransactionId: order.TransactionId,
		OutRefundNo:   order.OutRefundNo,
		Remark:        order.Remark,
		CreateTime:    order.CreateTime.Unix(),
		UpdateTime:    order.UpdateTime.Unix(),
		DelFlag:       order.DelFlag,
	}, nil
}
